//
// Created by zhou on 2022/6/13.
//
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MONTHS 12
char * s_gets(char *st, int n);
int get_days(char *st);
int days(int month);
struct month {
    char name[10];
    char abbrev[4];
    int days;
    int monumb;     // 月份号
};

struct month months[12] = {
        {"january", "jan", 31, 1},
        {"february", "feb", 28, 2},
        {"march","mar", 31, 3},
        {"april","apr", 30, 4},
        {"may", "may", 31, 5},
        {"june", "jun", 30, 6},
        {"july", "jul", 31, 7},
        {"august", "aug", 31, 8},
        {"september", "sep", 30, 9},
        {"october", "oct", 31, 10},
        {"november", "nov", 30, 11},
        {"december", "dec", 31, 12},
};

int days(int month)
{
    int index, total;
    if (month < 1 || month > 12)
        return (-1);
    else
    {
        for (index = 0, total = 0; index < month; index++)
            total += months[index].days;
        return (total);
    }
}

int get_days(char *st)
{
    int i = 0;
    int total = 0;
    int count;

    while (st[i])
    {
        st[i] = tolower(st[i]);   // 避免因大小写的原因
        i++;
    }

    for (i = 0; i < MONTHS; i++)
    {
        if (strcmp(st, months[i].name) == 0)
            break;
    }
    if (i < MONTHS)     // i 现在就是
    {
        for (count = 0; count <= i; count++)
            total += months[count].days;
    }

    return total;
}

int main(void)
{
    char st[10];

    printf("Please input the month name (empty line to quit):\n");
    while (s_gets(st, 10) != NULL && st[0] != '\0')
    {
        if (get_days(st) == 0)
            printf("The month name is wrong.\n");
        else
            printf("The days until %s: %d\n", st, get_days(st));
        printf("Next input (empty line to quit):\n");
    }

    printf("Bye.\n");

    return 0;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');        // 返回 \n 的指针
        if (find)                       // 如果地址不是NULL
            *find = '\0';               // 在此处放置一个空字符
        else            // 没有找到 \n
            while (getchar() != '\n')
                continue;               // 清理输入行中剩余的字符
    }

    return ret_val;
}